namespace SmartSoft.ADP
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class SSInvoiceLibDataAccess
    {
        private WSSSInvoiceLibDataAccess.WSSSInvoiceLibDataAccess wDataAcess = new WSSSInvoiceLibDataAccess.WSSSInvoiceLibDataAccess();
        private SmartSoft.DAL.SSInvoiceLibDataAccess oDataAcess = new SmartSoft.DAL.SSInvoiceLibDataAccess();

        public SSInvoiceLibDataAccess()
        {
            wDataAcess.Url = wDataAcess.Url.Replace("localhost", AdapterInstance.ServerIp);
        }

        public string CtVAT_So_Ct0_List(object ps_MasterKey)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.CtVAT_So_Ct0_List(ps_MasterKey);
            }
            else
            {
                return oDataAcess.CtVAT_So_Ct0_List(ps_MasterKey);
            }
        }

        public int CtVAT_Status_Get(object ps_Ma_Nvu, object ps_MasterKey)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.CtVAT_Status_Get(ps_Ma_Nvu, ps_MasterKey);
            }
            else
            {
                return oDataAcess.CtVAT_Status_Get(ps_Ma_Nvu, ps_MasterKey);
            }
        }

        //public bool DeletePayment(SqlConnection po_Connection, SqlTransaction po_Transaction, object ps_InvoiceOrder, object ps_Ma_Bp, object ps_Ma_Hd, object ps_Ma_Dt, object ps_No_Co, object ps_Tk, object ps_Ma_Tte, object pd_Han_Tt)
        //{
        //    if (AdapterInstance.IsRemoteConnected)
        //    {
        //        return wDataAcess.DeletePayment(po_Connection, po_Transaction, ps_InvoiceOrder, ps_Ma_Bp, ps_Ma_Hd, ps_Ma_Dt, ps_No_Co, ps_Tk, ps_Ma_Tte, pd_Han_Tt);
        //    }
        //    else
        //    {
        //        return oDataAcess.DeletePayment(po_Connection, po_Transaction, ps_InvoiceOrder, ps_Ma_Bp, ps_Ma_Hd, ps_Ma_Dt, ps_No_Co, ps_Tk, ps_Ma_Tte, pd_Han_Tt);
        //    }
        //}

        public decimal ExchangeRate(object ps_Ma_Tte)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.ExchangeRate(ps_Ma_Tte);
            }
            else
            {
                return oDataAcess.ExchangeRate(ps_Ma_Tte);
            }
        }

        public decimal ExchangeRate(object ps_Ma_Tte, ref bool pb_ExchangeType)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.ExchangeRate(ps_Ma_Tte, ref pb_ExchangeType);
            }
            else
            {
                return oDataAcess.ExchangeRate(ps_Ma_Tte, ref pb_ExchangeType);
            }
        }

        public void GetBankAccount(object ps_MasterKey, ref object ps_Ten_Dvi_A, ref object ps_Tk_NH_A, ref object ps_Ten_NH_A, ref object ps_Ten_TP_A, ref object ps_Ten_Dvi_B, ref object ps_Tk_NH_B, ref object ps_Ten_NH_B, ref object ps_Ten_TP_B)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                wDataAcess.GetBankAccount(ps_MasterKey, ref ps_Ten_Dvi_A, ref ps_Tk_NH_A, ref ps_Ten_NH_A, ref ps_Ten_TP_A, ref ps_Ten_Dvi_B, ref ps_Tk_NH_B, ref ps_Ten_NH_B, ref ps_Ten_TP_B);
            }
            else
            {
                oDataAcess.GetBankAccount(ps_MasterKey, ref ps_Ten_Dvi_A, ref ps_Tk_NH_A, ref ps_Ten_NH_A, ref ps_Ten_TP_A, ref ps_Ten_Dvi_B, ref ps_Tk_NH_B, ref ps_Ten_NH_B, ref ps_Ten_TP_B);
            }
        }

        public DataTable GetDetailHistoryTable(string ps_Ma_Nvu, object pi_HistoryID)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetDetailHistoryTable(ps_Ma_Nvu, pi_HistoryID);
            }
            else
            {
                table= oDataAcess.GetDetailHistoryTable(ps_Ma_Nvu, pi_HistoryID);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetHistoryTable(object ps_InvoiceOrder)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetHistoryTable(ps_InvoiceOrder);
            }
            else
            {
                table= oDataAcess.GetHistoryTable(ps_InvoiceOrder);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetHistoryTable(string ps_Subsidiary, string ps_InvoiceCode, object pd_Ngay_Ct1, object pd_Ngay_Ct2)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetHistoryTable(ps_Subsidiary, ps_InvoiceCode, pd_Ngay_Ct1, pd_Ngay_Ct2);
            }
            else
            {
                table= oDataAcess.GetHistoryTable(ps_Subsidiary, ps_InvoiceCode, pd_Ngay_Ct1, pd_Ngay_Ct2);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataRow GetInvoiceRow(string ps_InvoiceCode)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return SmartSoft.DAL.SSDataAccess.ConvertTableToRow(wDataAcess.GetInvoiceRow(ps_InvoiceCode));
            }
            else
            {
                return oDataAcess.GetInvoiceRow(ps_InvoiceCode);
            }
        }

        public DataTable GetInvoices(string ps_InvoiceCode)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetInvoices(ps_InvoiceCode);
            }
            else
            {
                table= oDataAcess.GetInvoices(ps_InvoiceCode);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataRow GetMasterHistoryDataRow(string ps_Ma_Nvu, object pi_HistoryID)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return SmartSoft.DAL.SSDataAccess.ConvertTableToRow(wDataAcess.GetMasterHistoryDataRow(ps_Ma_Nvu, pi_HistoryID));
            }
            else
            {
                return oDataAcess.GetMasterHistoryDataRow(ps_Ma_Nvu, pi_HistoryID);
            }
        }

        public DataTable GetMasterHistoryTable(string ps_Ma_Nvu, object ps_InvoiceOrder)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetMasterHistoryTable(ps_Ma_Nvu, ps_InvoiceOrder);
            }
            else
            {
                table= oDataAcess.GetMasterHistoryTable(ps_Ma_Nvu, ps_InvoiceOrder);
            }

            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetPaymentDetailTable(string ps_Subsidiary, object ps_InvoiceOrder, object ps_Ma_Bp, object ps_Ma_Hd, object ps_Ma_Dt, object ps_No_Co, object ps_Tk, object ps_Ma_Tte, object pd_Han_Tt, object pd_Ngay_Ct)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetPaymentDetailTable(ps_Subsidiary, ps_InvoiceOrder, ps_Ma_Bp, ps_Ma_Hd, ps_Ma_Dt, ps_No_Co, ps_Tk, ps_Ma_Tte, pd_Han_Tt, pd_Ngay_Ct);
            }
            else
            {
                table= oDataAcess.GetPaymentDetailTable(ps_Subsidiary, ps_InvoiceOrder, ps_Ma_Bp, ps_Ma_Hd, ps_Ma_Dt, ps_No_Co, ps_Tk, ps_Ma_Tte, pd_Han_Tt, pd_Ngay_Ct);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetPaymentMasterTable(object ps_InvoiceOrder)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetPaymentMasterTable(ps_InvoiceOrder);
            }
            else
            {
                table= oDataAcess.GetPaymentMasterTable(ps_InvoiceOrder);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public int InvoiceStatus(string ps_Subsidiary, string ps_Ma_Nvu, string ps_UserName, object ps_InvoiceOrder)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.InvoiceStatus(ps_Subsidiary, ps_Ma_Nvu, ps_UserName, ps_InvoiceOrder);
            }
            else
            {
                return oDataAcess.InvoiceStatus(ps_Subsidiary, ps_Ma_Nvu, ps_UserName, ps_InvoiceOrder);
            }
        }

        public void MakeAmountWarningMessage(string ps_Subsidiary, object ps_MasterKey, object pd_Ngay_Ct, object ps_Tk, ref string ps_Ten_Tk, ref string ps_Ten_Tk_E, object ps_Ma_Dt, ref string ps_No_Co, ref string ps_Ten_Dt, ref string ps_Ten_Dt_E, string ps_SQLCmd, ref decimal pi_Du_No, ref decimal pi_Du_Co, ref decimal pi_Tien_Lmt)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                wDataAcess.MakeAmountWarningMessage(ps_Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Tk, ref ps_Ten_Tk, ref ps_Ten_Tk_E, ps_Ma_Dt, ref ps_No_Co, ref ps_Ten_Dt, ref ps_Ten_Dt_E, ps_SQLCmd, ref pi_Du_No, ref pi_Du_Co, ref pi_Tien_Lmt);
            }
            else
            {
                oDataAcess.MakeAmountWarningMessage(ps_Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Tk, ref ps_Ten_Tk, ref ps_Ten_Tk_E, ps_Ma_Dt, ref ps_No_Co, ref ps_Ten_Dt, ref ps_Ten_Dt_E, ps_SQLCmd, ref pi_Du_No, ref pi_Du_Co, ref pi_Tien_Lmt);
            }
        }

        public void MakeLotQtyWarningMessage(string ps_Subsidiary, object ps_MasterKey, object pd_Ngay_Ct, object ps_Ma_Kho, ref string ps_Ten_Kho, ref string ps_Ten_Kho_E, object ps_Ma_Vt, ref string ps_Ten_Vt, ref string ps_Ten_Vt_E, ref string ps_Dvt, ref string ps_Dvt_E, object ps_Lo_Hang, string ps_SQLCmd, ref decimal pi_Ton_Kho)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                wDataAcess.MakeLotQtyWarningMessage(ps_Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho == DBNull.Value ? string.Empty : ps_Ma_Kho, ref ps_Ten_Kho, ref ps_Ten_Kho_E, ps_Ma_Vt == DBNull.Value ? string.Empty : ps_Ma_Vt, ref ps_Ten_Vt, ref ps_Ten_Vt_E, ref ps_Dvt, ref ps_Dvt_E, ps_Lo_Hang, ps_SQLCmd, ref pi_Ton_Kho);
            }
            else
            {
                oDataAcess.MakeLotQtyWarningMessage(ps_Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho, ref ps_Ten_Kho, ref ps_Ten_Kho_E, ps_Ma_Vt, ref ps_Ten_Vt, ref ps_Ten_Vt_E, ref ps_Dvt, ref ps_Dvt_E, ps_Lo_Hang, ps_SQLCmd, ref pi_Ton_Kho);
            }
        }

        public DataTable MakeLotQuantity(string ps_Subsidiary, object ps_MasterKey, object pd_Ngay_Ct, object ps_Ma_Kho, object ps_Ma_Vt, object ps_Lo_Hang, string ps_SQLCmd, int pi_Max_Rows)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.MakeLotQuantity(ps_Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho == DBNull.Value ? string.Empty : ps_Ma_Kho, ps_Ma_Vt == DBNull.Value ? string.Empty : ps_Ma_Vt, ps_Lo_Hang, ps_SQLCmd, pi_Max_Rows);
            }
            else
            {
                table= oDataAcess.MakeLotQuantity(ps_Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho, ps_Ma_Vt, ps_Lo_Hang, ps_SQLCmd, pi_Max_Rows);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable MakeOutLotQuantity(string ps_Subsidiary, object ps_MasterKey, object pd_Ngay_Ct, object ps_Ma_Kho, object ps_Ma_Vt, object ps_Lo_Hang, string ps_SQLCmd, int pi_Max_Rows)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.MakeOutLotQuantity(ps_Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho == DBNull.Value ? string.Empty : ps_Ma_Kho, ps_Ma_Vt == DBNull.Value ? string.Empty : ps_Ma_Vt, ps_Lo_Hang, ps_SQLCmd, pi_Max_Rows);
            }
            else
            {
                table= oDataAcess.MakeOutLotQuantity(ps_Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho, ps_Ma_Vt, ps_Lo_Hang, ps_SQLCmd, pi_Max_Rows);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public void MakeQtyWarningMessage(string ps_Subsidiary, object ps_MasterKey, object pd_Ngay_Ct, object ps_Ma_Kho, ref string ps_Ten_Kho, ref string ps_Ten_Kho_E, object ps_Ma_Vt, ref string ps_Ten_Vt, ref string ps_Ten_Vt_E, ref string ps_Dvt, ref string ps_Dvt_E, string ps_SQLCmd, ref decimal pi_Ton_Kho, ref decimal pi_SL_Ton_Min, ref decimal pi_SL_Ton_Max)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                wDataAcess.MakeQtyWarningMessage(ps_Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho == DBNull.Value ? string.Empty : ps_Ma_Kho, ref ps_Ten_Kho, ref ps_Ten_Kho_E, ps_Ma_Vt == DBNull.Value ? string.Empty : ps_Ma_Vt, ref ps_Ten_Vt, ref ps_Ten_Vt_E, ref ps_Dvt, ref ps_Dvt_E, ps_SQLCmd, ref pi_Ton_Kho, ref pi_SL_Ton_Min, ref pi_SL_Ton_Max);
            }
            else
            {
                oDataAcess.MakeQtyWarningMessage(ps_Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho, ref ps_Ten_Kho, ref ps_Ten_Kho_E, ps_Ma_Vt, ref ps_Ten_Vt, ref ps_Ten_Vt_E, ref ps_Dvt, ref ps_Dvt_E, ps_SQLCmd, ref pi_Ton_Kho, ref pi_SL_Ton_Min, ref pi_SL_Ton_Max);
            }
        }

        public DataTable MakeStockLotQuantity(string ps_Subsidiary, object ps_MasterKey, object pd_Ngay_Ct, object ps_Ma_Kho, object ps_Ma_Vt, string ps_SQLCmd)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.MakeStockLotQuantity(ps_Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho == DBNull.Value ? string.Empty : ps_Ma_Kho, ps_Ma_Vt == DBNull.Value ? string.Empty : ps_Ma_Vt, ps_SQLCmd);
            }
            else
            {
                table= oDataAcess.MakeStockLotQuantity(ps_Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho, ps_Ma_Vt, ps_SQLCmd);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable MakeStockLotQuantity(string ps_Subsidiary, object ps_MasterKey, object pd_Ngay_Ct, object ps_Ma_Kho, object ps_Ma_Ke, object ps_Ma_Vt, object ps_Lo_Hang, string ps_SQLCmd, int pi_Max_Rows)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.MakeStockLotQuantity(ps_Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho == DBNull.Value ? string.Empty : ps_Ma_Kho, ps_Ma_Ke, ps_Ma_Vt == DBNull.Value ? string.Empty : ps_Ma_Vt, ps_Lo_Hang, ps_SQLCmd, pi_Max_Rows);
            }
            else
            {
                table= oDataAcess.MakeStockLotQuantity(ps_Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho, ps_Ma_Ke, ps_Ma_Vt, ps_Lo_Hang, ps_SQLCmd, pi_Max_Rows);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable RefTable_Get(object ps_MasterKey)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.RefTable_Get(ps_MasterKey);
            }
            else
            {
                table= oDataAcess.RefTable_Get(ps_MasterKey);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public void ResetInvoiceNumber(string ps_StoredProcedure, string ps_Subsidiary, string ps_InvoiceCode, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object ps_So_Ct0, object pi_So_Ct1)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                wDataAcess.ResetInvoiceNumber(ps_StoredProcedure, ps_Subsidiary, ps_InvoiceCode, pd_Ngay_Ct1, pd_Ngay_Ct2, ps_So_Ct0, pi_So_Ct1);
            }
            else
            {
                oDataAcess.ResetInvoiceNumber(ps_StoredProcedure, ps_Subsidiary, ps_InvoiceCode, pd_Ngay_Ct1, pd_Ngay_Ct2, ps_So_Ct0, pi_So_Ct1);
            }
        }

        public void Restore(string ps_Ma_Nvu, string ps_UserName, object ps_InvoiceOrder, object pi_HistoryID)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                wDataAcess.Restore(ps_Ma_Nvu, ps_UserName, ps_InvoiceOrder, pi_HistoryID);
            }
            else
            {
                oDataAcess.Restore(ps_Ma_Nvu, ps_UserName, ps_InvoiceOrder, pi_HistoryID);
            }
        }

        public bool Save(string ps_Ma_Nvu, DataRow po_MasterDataRow, DataTable po_DetailTable)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.Save(ps_Ma_Nvu, SmartSoft.DAL.SSDataAccess.ConvertRowToTable(po_MasterDataRow), po_DetailTable);
            }
            else
            {
                return oDataAcess.Save(ps_Ma_Nvu, po_MasterDataRow, po_DetailTable);
            }
        }

        public void SaveBankAccount(object ps_MasterKey, object ps_Ten_Dvi_A, object ps_Tk_NH_A, object ps_Ten_NH_A, object ps_Ten_TP_A, object ps_Ten_Dvi_B, object ps_Tk_NH_B, object ps_Ten_NH_B, object ps_Ten_TP_B)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                wDataAcess.SaveBankAccount(ps_MasterKey, ps_Ten_Dvi_A, ps_Tk_NH_A, ps_Ten_NH_A, ps_Ten_TP_A, ps_Ten_Dvi_B, ps_Tk_NH_B, ps_Ten_NH_B, ps_Ten_TP_B);
            }
            else
            {
                oDataAcess.SaveBankAccount(ps_MasterKey, ps_Ten_Dvi_A, ps_Tk_NH_A, ps_Ten_NH_A, ps_Ten_TP_A, ps_Ten_Dvi_B, ps_Tk_NH_B, ps_Ten_NH_B, ps_Ten_TP_B);
            }
        }

        //public bool SavePayment(SqlConnection po_Connection, SqlTransaction po_Transaction, object ps_InvoiceOrder, object ps_Ma_Bp, object ps_Ma_Hd, object ps_Ma_Dt, object ps_No_Co, object ps_Tk, object ps_Ma_Tte, object pi_Tien_Nt, object pd_Han_Tt0, object pd_Han_Tt1, object ps_DOrder)
        //{
        //    if (AdapterInstance.IsRemoteConnected)
        //    {
        //        return wDataAcess.SavePayment(po_Connection, po_Transaction, ps_InvoiceOrder, ps_Ma_Bp, ps_Ma_Hd, ps_Ma_Dt, ps_No_Co, ps_Tk, ps_Ma_Tte, pi_Tien_Nt, pd_Han_Tt0, pd_Han_Tt1, ps_DOrder);
        //    }
        //    else
        //    {
        //        return oDataAcess.SavePayment(po_Connection, po_Transaction, ps_InvoiceOrder, ps_Ma_Bp, ps_Ma_Hd, ps_Ma_Dt, ps_No_Co, ps_Tk, ps_Ma_Tte, pi_Tien_Nt, pd_Han_Tt0, pd_Han_Tt1, ps_DOrder);
        //    }
        //}
    }
}